package com.gitee.xmhzzz.component.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

/**
  * @ClassName
  * @Description
  * @Author wzq
  * @Date 2023/12/15 17:05
  * @Version 1.0
  */
@Getter
@AllArgsConstructor
public enum ResponseCode implements ErrorCode
{

    /**
     *
     */
    SUCCESS(200, "成功"),
    FAIL(101, "失败"),
    INTERNAL_SERVER_ERROR(500, "服务器内部系统未知异常"),
    SYSTEM_BUSY(1000, "系统繁忙"),
    BUSINESS_EXCEPTION(1001, "业务异常"),
    QUERY_FAIL(1002, "查询失败"),
    FAIL_LOCK(1004, "获取锁失败"),
    BAD_REQUEST(400, "参数错误"),
    NO_LOGIN(401, "未登录"),
    NO_PERMISSION(403, "无权限"),
    NOT_FOUND(404, "资源不存在"),
    UNSUPPORTED_MEDIA_TYPE(415, "不支持的媒体类型(Content-Type 或 Content-Encoding)"),
    METHOD_NOT_ALLOWED(405, "请求的HTTP方法不允许"),
    MISSING_REQ_HEADER(445, "缺失必要的请求头(Headers)"),
    CONNECT_EXCEPTION(530, "服务连接异常"),
    NULL_POINTER_EXCEPTION(550, "服务器内部空指针异常"),
    DATABASE_EXCEPTION(551, "服务器内部数据库发生异常"),
    SQL_EXCEPTION(552, "服务器内部数据库SQL执行异常"),
    BEAN_INIT_EXCEPTION(553, "Bean 实例化异常"),
    DATABASE_SHARDING_ROUTE(554, "服务器内部数据库分库分表路由异常"),
    DEPRECATED_METHOD_EXCEPTION(555, "调用废弃方式导致的异常"),
    FALLBACK_EXCEPTION(556, "降级异常"),
    DUPLICATE_KEY_EXCEPTION(557, "服务器内部数据库唯一索引异常"),
    UNKNOWN(9999999, "未知错误");

    /**
     * 状态码
     */
    private final Integer code;
    /**
     * 状态描述
     */
    private final String msg;


    public static ResponseCode of(Integer code) {
        return Arrays.stream(ResponseCode.values()).filter(values -> values.code.equals(code)).findFirst().orElse(UNKNOWN);
    }

    public static ResponseCode ofNull(Integer code) {
        return Arrays.stream(ResponseCode.values()).filter(values -> values.code.equals(code)).findFirst().orElse(null);
    }

}
